import router from '@/router';
import { SvgIcon } from '@jl/common';
import { svgIconPlugin } from '@jl/common/plugin';
import { useUserStore } from '@jl/common/store';
import { InvalidCodeEnum, setGlobalRequestConfig } from '@jl/common/utils';
import { messageUtil } from '@jl/element-plus/utils';
import { App } from 'vue';

export const installPlugin = (app: App) => {
  app.component('SvgIcon', SvgIcon);
  app.use(svgIconPlugin);

  setGlobalRequestConfig({
    timeout: 60000,
    messageUtil,
    // requestInterceptor(config: any) {
    //   config.Connection = null;
    // },
    responseInterceptor(data) {
      const userStore = useUserStore();
      const { code, msg } = data;

      // 票据失效
      if (InvalidCodeEnum[+code]) {
        router.push('/login');
        userStore.reset();
        messageUtil.error(msg);
        return false;
      }
      // 分享
      if (+code === -90111001) {
        router.push({ path: '/share/noauth', query: { id: 1 } });
        return false;
      }
      if (+code === -90111003) {
        router.push({ path: '/share/noauth', query: { id: 2 } });
        return false;
      }
      if (+code === -90111004) {
        router.push(`/share/prepare?s_url=${location.pathname.replace('/cimup', '')}`);
        return false;
      }
      return true;
    },
  });
};
